


//
//  SJNetStatusTool.m
//  GuTouBao
//
//  Created by zhaoshijie on 2018/6/14.
//  Copyright © 2018 zhaoshijie. All rights reserved.
//

#import "SJNetStatusTool.h"
#import "Reachability.h"
#import "AFNetworking.h"
#import "AFNetworkActivityIndicatorManager.h"

@implementation SJNetStatusTool




/// 获取网络状态
+ (NSString *)gainNetStatusTool
{
    
    Reachability *reachability   = [Reachability reachabilityWithHostName:@"www.apple.com"];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    NSString *net = @"WIFI";
    switch (internetStatus) {
        case ReachableViaWiFi:
            net = @"WIFI";
            break;
            
        case ReachableViaWWAN:
            net = @"4G";
            //net = [self getNetType ];   //判断具体类型
            break;
            
        case NotReachable:
            net = @"当前无网路连接";
            
        default:
            break;
    }
    
    return net;
}


/// 获取网络状态
+ (NSInteger)gainNetStatusToolInType
{
    
    Reachability *reachability   = [Reachability reachabilityWithHostName:@"www.apple.com"];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    NSInteger statusType = 0;
    switch (internetStatus) {
        case ReachableViaWiFi:
        {
            //  wifi
            statusType = 1;
        }
            break;
        case ReachableViaWWAN:
        {
            //  4G
            statusType = 1;
        }
            break;
        case NotReachable:
        {
            //  当前无网路连接
            statusType = 0;
        }
        default:
            break;
    }
    
    return statusType;
}


- (NSString *)getNetType
{
    
    //  当用户为手机自带网络时，判断具体网络类型
    //  这时可以使用系统自带的API来判断是2G、3G还是4G，这就要使用到CTTelephonyNetworkInfo这类，记得在工程添加CoreTelephony.framework，并导入头文件、
    
    /*
    CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
    NSString *currentStatus = info.currentRadioAccessTechnology;
    if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyGPRS"]) {
        netconnType = @"GPRS";
    }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyEdge"]) {
        netconnType = @"2.75G EDGE";
    }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyWCDMA"]){
        netconnType = @"3G";
    }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSDPA"]){
        netconnType = @"3.5G HSDPA";
    }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSUPA"]){
        netconnType = @"3.5G HSUPA";
    }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMA1x"]){
        netconnType = @"2G";
    }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORev0"]){
        netconnType = @"3G";
    }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevA"]){
        netconnType = @"3G";
    }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevB"]){
        netconnType = @"3G";
    }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyeHRPD"]){
        netconnType = @"HRPD";
    }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyLTE"]){
        netconnType = @"4G";
    }
     */
    
    return @"";
}



/// 检测网络状态变化
/// @param networkChangeBlock 变化回调，0表示没网络，1表示有
+(void)netWorkChangeEventWithNetworkChangeBlock:(void(^)(NSInteger status))networkChangeBlock
{
    if(!networkChangeBlock)
    {
        return;
    }
    
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url];
//    self.netWorkStatesCode = AFNetworkReachabilityStatusUnknown;
    [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//        self.netWorkStatesCode = status;
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:

            {
                //  NSLog(@"当前使用的是流量模式");
                networkChangeBlock(1);
                
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
            {
                //  NSLog(@"当前使用的是wifi模式");
                networkChangeBlock(1);
            }
                
                break;
            case AFNetworkReachabilityStatusNotReachable:
            {
                //  NSLog(@"断网了");
                networkChangeBlock(0);
            }
                break;
            case AFNetworkReachabilityStatusUnknown:
            {
                //  NSLog(@"变成了未知网络状态");
                networkChangeBlock(0);
                
            }
                break;
                
            default:
                break;
        }
    }];
    [manager.reachabilityManager startMonitoring];
    
}



@end
